﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using log4net;
namespace Task.Log
{
    /// <summary>
    /// 记录本地日志
    /// </summary>
    public class LogHelper
    {
        /// <summary>  
        /// 静态类  
        /// </summary>  
        private LogHelper() {   }
        private const string LOG_REPOSITORY = "Default"; // this should likely be set in the web config.  
        private static ILog m_log;
        /// <summary>  
        /// 初始化日志系统  
        /// 在系统运行开始初始化  
        /// Global.asax Application_Start内  
        /// </summary>  
        public static void Init()
        {
            log4net.Config.XmlConfigurator.Configure();
        }
        /// <summary>  
        /// 写入日志  
        /// </summary>  
        /// <param name="message">日志信息</param>  
        /// <param name="messageType">日志类型</param>  
        /// <param name="type"></param>  
        public static void Write(string message, LogMessageType messageType, Type type)
        {
            DoLog(message, messageType, null, type);
        }
        /// <summary>  
        /// 写入日志  
        /// </summary>  
        /// <param name="message">日志信息</param>  
        /// <param name="messageType">日志类型</param>  
        /// <param name="ex">异常</param>  
        /// <param name="type">System.Reflection.MethodBase.GetCurrentMethod().DeclaringType</param>  
        public static void Write(string message, LogMessageType messageType, Exception ex,
                                 Type type)
        {
            DoLog(message, messageType, ex, type);
        }
       /// <summary>
       /// 
       /// </summary>
       /// <param name="message"></param>
        /// <param name="type">System.Reflection.MethodBase.GetCurrentMethod().DeclaringType</param>
        public static void Info(string message, Type type)
        {
            DoLog(message, LogMessageType.Info, null, type);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="ex"></param>
        /// <param name="type">System.Reflection.MethodBase.GetCurrentMethod().DeclaringType</param>
        public static void Info(string message, Exception ex, Type type)
        {
          
            DoLog(message, LogMessageType.Info, ex, type);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="type">System.Reflection.MethodBase.GetCurrentMethod().DeclaringType</param>
        public static void Debug(string message, Type type)
        {
            DoLog(message, LogMessageType.Debug, null, Type.GetType("System.Object"));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="ex"></param>
        /// <param name="type">System.Reflection.MethodBase.GetCurrentMethod().DeclaringType</param>
        public static void Debug(string message, Exception ex, Type type)
        {
            DoLog(message, LogMessageType.Debug, ex, type);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="type">System.Reflection.MethodBase.GetCurrentMethod().DeclaringType</param>
        public static void Error(string message, Type type)
        {
            DoLog(message, LogMessageType.Error, null, Type.GetType("System.Object"));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="ex"></param>
        /// <param name="type">System.Reflection.MethodBase.GetCurrentMethod().DeclaringType</param>
        public static void Error(string message, Exception ex, Type type)
        {
            DoLog(message, LogMessageType.Error, ex, type);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="type">System.Reflection.MethodBase.GetCurrentMethod().DeclaringType</param>
        public static void Fatal(string message, Type type)
        {
            DoLog(message, LogMessageType.Fatal, null, Type.GetType("System.Object"));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="ex"></param>
        /// <param name="type">System.Reflection.MethodBase.GetCurrentMethod().DeclaringType</param>
        public static void Fatal(string message, Exception ex, Type type)
        {
            DoLog(message, LogMessageType.Fatal, ex, type);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="type">System.Reflection.MethodBase.GetCurrentMethod().DeclaringType</param>
        public static void Warn(string message, Type type)
        {
            DoLog(message, LogMessageType.Warn, null, Type.GetType("System.Object"));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="ex"></param>
        /// <param name="type">System.Reflection.MethodBase.GetCurrentMethod().DeclaringType</param>
        public static void Warn(string message, Exception ex, Type type)
        {
            DoLog(message, LogMessageType.Warn, ex, type);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="message"></param>
        /// <param name="type">System.Reflection.MethodBase.GetCurrentMethod().DeclaringType</param>
        public static void Assert(bool condition, string message, Type type)
        {
            if (condition == false)
                Write(message, LogMessageType.Info, type);
        }
       
        /// <summary>  
        /// 保存日志  
        /// </summary>  
        /// <param name="message">日志信息</param>  
        /// <param name="messageType">日志类型</param>  
        /// <param name="ex">异常</param>  
        /// <param name="type">日志类型</param>  
        private static void DoLog(string message, LogMessageType messageType, Exception ex,
                                  Type type)
        {
            //NlogLogger logger = new NlogLogger("测试");
          
            m_log = LogManager.GetLogger(type);//MethodBase.GetCurrentMethod().DeclaringType
            switch (messageType)
            {
                case LogMessageType.Debug:
                    //logger.Debug(message, ex);
                    LogHelper.m_log.Debug(message, ex);
                    break;
                case LogMessageType.Info:
                    //logger.Info(message, ex);
                    LogHelper.m_log.Info(message, ex);
                    break;
                case LogMessageType.Warn:
                    //logger.Warn(message,ex);
                    LogHelper.m_log.Warn(message, ex);
                    break;
                case LogMessageType.Error:
                    //logger.Error(message, ex);
                    //LogHelper.m_log.Error(message, ex);
                    break;
                case LogMessageType.Fatal:
                    //logger.Fatal(message, ex);
                    LogHelper.m_log.Fatal(message, ex);
                    break;
            }
        }
        /// <summary>  
        /// 日志类型  
        /// </summary>  
        public enum LogMessageType
        {
            /// <summary>  
            /// 调试  
            /// </summary>  
            Debug,
            /// <summary>  
            /// 信息  
            /// </summary>  
            Info,
            /// <summary>  
            /// 警告  
            /// </summary>  
            Warn,
            /// <summary>  
            /// 错误  
            /// </summary>  
            Error,
            /// <summary>  
            /// 致命错误  
            /// </summary>  
            Fatal
        }
    }
}

